package com.link.base.ext.autocfg.controller;


import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.base.ext.autocfg.model.AutoCfgItemSub;
import com.link.base.ext.autocfg.service.AutoCfgItemSubService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字段扩展配置，高级配置模块（字段配置的子项）
 * @author 姚润发
 * @date 2019/12/13
 */
@Controller("AutoCfgItemSubController")
@RequestMapping("/link/autoCfgItemSub")
public class AutoCfgItemSubController extends BasicController<AutoCfgItemSub> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AutoCfgItemSubService autoCfgItemSubService;

    private static final Logger logger = LogManager.getLogger(AutoCfgItemSubController.class);

    @Override
    public BasicService<AutoCfgItemSub> getBasicService() throws Exception {
        return autoCfgItemSubService;
    }

    /**
     * <p>更新与当前标准记录关联的记录信息</p>
     *
     * @param record
     * @return
     * @author yrf
     */
    @RequestMapping("/updateLinkRecord")
    @ResponseBody
    public Map<String, Object> updateLinkItemSub(AutoCfgItemSub record) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //已不用手动调用，更新时记录会断开与母版的关联
            //autoCfgItemSubService.updateLinkItemSub(record);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("AutoCfgItemSubController捕获异常: ",e);
            result.put("success", false);
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>增删改批量操作</p>
     *
     * @param list
     * @param request
     * @return
     * @author yrf
     */
    @RequestMapping(value = "/batchCRUD")
    @ResponseBody
    public Map<String, Object> crud(@RequestBody List<AutoCfgItemSub> list, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //autoCfgItemService.upsertAndDel(list);
            autoCfgItemSubService.upsertAndDel(list);
            result.put("success", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("AutoCfgItemSubController捕获异常: ",e);
            result.put("success", false);
            result.put("result", e.getMessage());
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
        }
        return result;
    }
}
